#!/usr/bin/env python
# -*- coding:UTF-8 -*-

"""
拉勾网测试爬虫
"""

import requests
from bs4 import BeautifulSoup
import json
import time

# 网址：
# https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=

def main():
    headers = {
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "Content-Length": "25",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "Cookie": "JSESSIONID=ABAAABAABEEAAJAAD54EAF67FB1B4BF174DE6845627F03A; user_trace_token=20191015114659-bcd75e91-4d34-4b9c-86ef-63f82430916e; WEBTJ-ID=20191015114653-16dcd869aa21d6-06fe9257b62d86-e343166-2073600-16dcd869aa33bc; _ga=GA1.2.249272177.1571111214; _gid=GA1.2.1032341859.1571111214; sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2216dcd869b35b02-0ea6bbf10fe1d9-e343166-2073600-16dcd869b369cd%22%2C%22%24device_id%22%3A%2216dcd869b35b02-0ea6bbf10fe1d9-e343166-2073600-16dcd869b369cd%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1571111214; LGUID=20191015114700-71a5dd7e-eefe-11e9-9bd0-525400f775ce; _gat=1; LGSID=20191015140111-30345063-ef11-11e9-9c0c-525400f775ce; PRE_UTM=; PRE_HOST=; PRE_SITE=; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Fjobs%2Flist_python%3FlabelWords%3D%26fromSearch%3Dtrue%26suginput%3D; X_HTTP_TOKEN=aff817118e9d3c0a49791117515e7fe89425ac7b6c; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1571119788; LGRID=20191015140954-68478b09-ef12-11e9-9c0c-525400f775ce; index_location_city=%E5%85%A8%E5%9B%BD; TG-TRACK-CODE=index_search; SEARCH_ID=0406de4a1bfb4afeaadc8fc73b65764f",
        "Host": "www.lagou.com",
        "Origin": "https://www.lagou.com",
        "Referer": "https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
        "X-Anit-Forge-Code": "0",
        "X-Anit-Forge-Token": None,
        "X-Requested-With": "XMLHttpRequest"
    }

    positionList = []
    positionDetailList = [] # 职位详情

    for i in range(1, 6):
        formData = {
            "first": "true",
            "pn": i,
            "kd": "python"
        }
        result = requests.post("https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false", headers=headers, data=formData)
        # print(result.content)
        print(result.json())
        time.sleep(2)

if __name__ == '__main__':
    main()
